package com.mrper.ext.adapter.goods;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.miguo.bean.GoodsBean;
import com.miguo.ui.R;
import com.miguo.ui.databinding.GriditemGenericGoodsBinding;
import com.mrper.ext.adapter.TempleteAdapter;
import com.mrper.util.DensityUtil;
import com.mrper.util.DeviceUtil;
import com.mrper.util.extra.GoodsUtil;

import java.util.List;

/**
 * Created by admin on 2016/3/14.
 * 通用的商品数据适配器
 */
public class GoodsGenericAdapter extends TempleteAdapter<GriditemGenericGoodsBinding,GoodsBean> implements TempleteAdapter.OnInitAdapterListener<GriditemGenericGoodsBinding,GoodsBean>{

    private int imageHeight = 0;//图片高度

    public GoodsGenericAdapter(Context context) {
        this(context, null);
    }

    public GoodsGenericAdapter(Context context, List<GoodsBean> source) {
        super(context, R.layout.griditem_generic_goods, source);
        imageHeight = (int)(DeviceUtil.getScreenWidth(context) - DensityUtil.dip2px(context,21.0f))/2;
        setOnInitAdapterListener(this);
    }

    @Override
    public void initSetValue(final GriditemGenericGoodsBinding binder, final GoodsBean item, int position) {
        binder.setGoodsInfo(item);
        ViewGroup.LayoutParams imglp = binder.imgGoods.getLayoutParams();
        imglp.height = imageHeight;
        binder.imgGoods.setLayoutParams(imglp);
        final boolean isFavor = GoodsUtil.isFavor(context,item);
        binder.setIsFavor(isFavor);
        binder.imgFavor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GoodsUtil.favorActionWidthImageView(context,binder.imgFavor,item,!isFavor);
                notifyDataSetChanged();//刷新数据
            }
        });
    }

}
